<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/31
 * Time: 17:03
 */

namespace common\sources\write\businessdepositrecord;

use Yii;
use common\models\craftsman\BusinessDepositRecord;

class BusinessDepositRecordAccess implements IBusinessDepositRecordAccess
{
	public function addDepositRecord($data) {
		return Yii::$app->db->createCommand()->insert('business_deposit_record', [
			'bus_id'         => $data['bus_id'],
			'task_commod_id' => isset($data['task_commod_id']) ? $data['task_commod_id'] : 0,
			'task_id'        => isset($data['task_id']) ? $data['task_id'] : 0,
			"task_order_id" => isset($data['task_order_id']) ? $data['task_order_id'] : 0,
			"task_order_num" => isset($data['task_order_num']) ? $data['task_order_num'] : '',
			'expend'         => $data['expend'],
			'income'         => $data['income'],
			'balance'        => $data['balance'],
			"freeze_change"  => isset($data['freeze_change']) ? $data['freeze_change'] : 0,
			"freeze_balance" => isset($data['freeze_balance']) ? $data['freeze_balance'] : 0,
			'remark'         => $data['remark'],
			"type"           => $data['type'],
			"tran_num"       => isset($data['tran_num']) ? $data['tran_num'] : '',
			"img_url"        => isset($data['img_url']) ? $data['img_url'] : '',
			"operatorId"     => isset($data['operatorId']) ? $data['operatorId'] : '',
			"action_type"    => isset($data['action_type']) ? $data['action_type'] : 0,
			'create_time'    => time(),
			'auditor_time'   => isset($data['auditor_user']) ? time() : '',
			'auditor_user'   => isset($data['auditor_user']) ? $data['auditor_user'] : '',
			'end_time'       => isset($data['end_user']) ? time() : '',
			'end_user'       => isset($data['end_user']) ? $data['end_user'] : '',
		])->execute();
	}

    public function addChangeDepositRecord($data) {
        $record = new BusinessDepositRecord();
        switch ($data['action_type']) {
            case 1://充值
                $record->type = $data['type'];
                $record->bus_id = $data['id'];
                $record->income = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 2://扣减
                $record->type = $data['type'];
                $record->bus_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 3://冻结
                $record->bus_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 4://解冻
                $record->bus_id = $data['id'];
                $record->income = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            case 5://扣除解冻
                $record->bus_id = $data['id'];
                $record->expend = $data['money'];
                $record->balance = $data['balance'];
                $record->remark = $data['remark'];
                $record->img_url = $data['img_url'];
                $record->operatorId = $data['operatorId'];
                $record->action_type = $data['action_type'];
                $record->task_id = $data['task_code'];
                $record->create_time = time();
                $record->update_time = time();
                break;
            default:
                break;
        }
        if ($record->save()) {
            return true;
        }
        return false;
    }

    public function addBalanceRecord($data) {
        $pepperRecord = new BusinessDepositRecord();
        $pepperRecord->bus_id = $data['id'];
        $pepperRecord->income = $data['money'];
        $pepperRecord->balance = $data['total_deposit'];
        $pepperRecord->remark = $data['remark'];
        $pepperRecord->operatorId = $data['operatorId'];
        $pepperRecord->order_id = $data['paycode'];
        if (isset($data['img_url'])) {
            $pepperRecord->img_url = $data['img_url'];
        }
        $pepperRecord->action_type = 1;//充值
        $pepperRecord->create_time = time();
        $pepperRecord->update_time = time();
        $pepperRecord->save();
    }

    public function addDepositRecord_pop($data) {
        $depositRecord = new BusinessDepositRecord();
        $depositRecord->bus_id = $data['pop_role_id'];
        $depositRecord->expend = $data['deposit'];
        $depositRecord->balance = $data['remail_deposit'];
        $depositRecord->remark = $data['remark'];
        $depositRecord->operatorId = $data['operatorId'];
        $depositRecord->task_id = $data['task_id'];
        $depositRecord->img_url = $data['img_url'];
        $depositRecord->action_type = 2;//扣减
        $depositRecord->create_time = time();
        $depositRecord->update_time = time();
        $depositRecord->save();
    }

    public function addDepositRecord_push($data) {
        $depositRecord = new BusinessDepositRecord();
        $depositRecord->bus_id = $data['push_role_id'];
        $depositRecord->income = $data['deposit'];
        $depositRecord->balance = $data['push_deposit'];
        $depositRecord->remark = $data['remark'];
        $depositRecord->operatorId = $data['operatorId'];
        $depositRecord->task_id = $data['task_id'];
        $depositRecord->img_url = $data['img_url'];
        $depositRecord->action_type = 1;//充值
        $depositRecord->create_time = time();
        $depositRecord->update_time = time();
        $depositRecord->save();
    }

    public function addBusDepositRecord($data)
    {
        return Yii::$app->db->createCommand()->insert('business_deposit_record', [
            'bus_id' =>  $data['bus_id'],
            'task_commod_id' => $data['task_commod_id'],
            'task_id' => $data['task_id'],
            'task_order_id' => $data['task_order_id'],
            'task_order_num' => $data['task_order_num'],
            'type' => $data['type'],
            'expend' => $data['expend'],
            'income' => $data['income'],
            'balance' => $data['balance'],
            'freeze_change' => isset($data['freeze_change']) ? $data['freeze_change'] : 0 ,
            'freeze_balance' => isset($data['freeze_balance']) ? $data['freeze_balance'] : 0 ,
            'remark' => $data['remark'],
            'operatorId' => isset($data['operatorId']) ? $data['operatorId'] : '',
            'create_time' => time()
        ])->execute();
    }
    public function addBuyDepositRecord($data)
    {
        // TODO: Implement addBuyDepositRecord() method.
    }
}